package ar.com.unlp.fi.lrs.service;

import java.util.List;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ar.com.unlp.fi.lrs.domain.Actor;
import ar.com.unlp.fi.lrs.domain.Agent;
import ar.com.unlp.fi.lrs.domain.Group;
import ar.com.unlp.fi.lrs.persist.ActorDao;

@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ActorService extends AbstractService {

	private ActorDao actorDao = new ActorDao();

	/**
	 * 
	 * @param actor
	 */
	
	public void create(Actor actor) {

		actorDao.save(actor);

	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	public Actor get(long id) {
		return actorDao.get(id);
	}

	/**
	 * 
	 * @param idGroup
	 * @param idAgent
	 */
	@Transactional
	public void addAgentToGroup(long idGroup, long idAgent) {

		Agent agent = (Agent) actorDao.get(idAgent);
		Group group = (Group) actorDao.get(idGroup);

		group.getMembers().add(agent);

		actorDao.update(group);

	}

	/**
	 * Retorna todos los grupos a los que un Actor pertenece
	 * 
	 * @param idActor
	 * @return
	 */
	public List<Group> getGroups(long idActor) {

		return actorDao.getGroups(idActor);

	}

}
